package org.ruoyi.common.chat.config;

import org.ruoyi.common.chat.handler.CommonChatWebSocketHandler;
import org.ruoyi.common.chat.handler.WebSocketMessageHandler;
import org.ruoyi.common.chat.interceptor.PlusWebSocketInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.HandshakeInterceptor;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    private final WebSocketMessageHandler messageHandler;

    public WebSocketConfig(WebSocketMessageHandler messageHandler) {
        this.messageHandler = messageHandler;
    }

    @Bean
    public HandshakeInterceptor tokenHandshakeInterceptor() {
        return new PlusWebSocketInterceptor();
    }
    @Bean
    public CommonChatWebSocketHandler commonChatWebSocketHandler() {
        return new CommonChatWebSocketHandler(messageHandler);
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(commonChatWebSocketHandler(), "resource/websocket")
                .setAllowedOrigins("*");
    }
}
